package controllers;

import play.*;
import play.mvc.*;

import java.text.SimpleDateFormat;
import java.util.*;

import org.joda.time.DateMidnight;
import org.joda.time.DateTime;
import org.joda.time.DateTimeConstants;

import models.*;

public class Application extends Controller {

    public static void index() {
    	List<models.Event> events = models.Event.findAll();
    	Application.calendrier();
        render(events);
    }
    
    public static void calendrier()
    {
    	DateTime now = new DateTime();
    	int annee = now.getYear();
    	int week = now.getWeekOfWeekyear();
    	Application.week(annee,week);
    }
    
    public static void week(int annee,int semaine)
    {
    	DateTime debutSemaine = new DateTime();
    	debutSemaine = debutSemaine.withDayOfWeek(DateTimeConstants.MONDAY);
    	debutSemaine = debutSemaine.withYear(annee);
    	debutSemaine = debutSemaine.withWeekOfWeekyear(semaine);
    	
    	
    	DateTime finSemaine = new DateTime();
    	finSemaine = finSemaine.withYear(annee);
    	finSemaine = finSemaine.withWeekOfWeekyear(semaine);
    	finSemaine = finSemaine.withDayOfWeek(DateTimeConstants.SUNDAY);
    	
    	
    	String moisDebut = debutSemaine.monthOfYear().getAsText(java.util.Locale.ENGLISH);
    	int joursDebut = debutSemaine.dayOfMonth().get();
		int yearDebut = debutSemaine.year().get();
		
		
		String moisFin = finSemaine.monthOfYear().getAsText(java.util.Locale.ENGLISH);
		int joursFin = finSemaine.dayOfMonth().get();
		int yearFin = finSemaine.year().get();
		//-------------Placer les évenements dans les cases appropriés 
		// On recupere d'abord les évenements 
		List<models.Event> events = models.Event.findAll();
		
		
    	render(debutSemaine,finSemaine,moisDebut,joursDebut,yearDebut,moisFin,joursFin,yearFin,events);
    }
    
    public static void events_json() {
    	renderJSON(models.Event.findAll());
    }
    
    public static void create_event() {
    	
    	render();
    }
    
public static void process_create_event(String name, String begin, String heureBegin, String heureEnd) {
    	
    	Event event = new Event();
    	event.name = name;
    	
    	String msg="";
    	
    	try {
    		// date de début = date de fin
        	event.begin = new SimpleDateFormat("dd-mm-yyyy").parse(begin);
        	event.end = new SimpleDateFormat("dd-mm-yyyy").parse(begin);
        	
        	String[] splitStartat = heureBegin.split(":");
            int startAtHours = Integer.parseInt(splitStartat[0]);
            int startAtMinutes = Integer.parseInt(splitStartat[1]);
            
            String[] splitEndat = heureEnd.split(":");
            int EndAtHours = Integer.parseInt(splitEndat[0]);
            int EndAtMinutes = Integer.parseInt(splitEndat[1]);
            
            DateTime eventStartAt = new DateTime();
            eventStartAt = eventStartAt.withTime(startAtHours, startAtMinutes, 0, 0);

            DateTime eventEndAt = new DateTime();
            
            eventEndAt = eventEndAt.withTime(EndAtHours, EndAtMinutes, 0, 0);
            
            if(startAtHours*60+startAtMinutes<=EndAtHours*60+EndAtMinutes){
            	event.save();
        		
        		msg = "Creation of the event is a success! :D";
            } else{
            	System.out.println("Erreur : date de commencement supérieure à celle de fin");
            } 
    		
    	} catch(Exception e) {
    		e.printStackTrace();
    		msg = "Error during the creation of the event :(";
    	}
    	String[] frenchDateFormatBegin = begin.toString().split("-");
    	
    	String beginFrenchDateFormat = frenchDateFormatBegin[2]+"/"+
    			frenchDateFormatBegin[1]+"/"+frenchDateFormatBegin[0];
    	render(name, beginFrenchDateFormat, msg, heureBegin, heureEnd);
    }
	public static void Evenement()
	{
		// On doit charger l'evenement en question 
		List<models.Event> events = models.Event.findAll();
	}
	
	public static void putEvent(String id){
		
		System.out.println(id);
		//int  id_long =Integer.parseInt(id);
		long id1 = Long.parseLong(id.toString());
		Event event = Event.findById(id1);
		
		render(event);
	}
	public static void process_delete(long id_event)
	{
		Event event = Event.findById(id_event);
		event.delete();
		render();
	}
	
	public void show(){
		render();
	}
	public static void crud(){
	render();
}
}